package com.xiangban.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiangban.dao.RelationMapper;
import com.xiangban.exception.BizException;
import com.xiangban.model.Relation;
import com.xiangban.model.dto.RelationDto;
import com.xiangban.model.res.ApiResult;
import com.xiangban.model.res.ApiResultCode;
import com.xiangban.model.res.Ids;
import com.xiangban.service.RelationService;
import com.xiangban.utils.CommonUtils;
import com.xiangban.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;

import static com.xiangban.utils.CommonUtils.BizEmpty;

/**
 * @Author Mr. MaYaYun
 * @Data 2021/12/6 15:17
 */
@Service
public class RelationServiceImpl implements RelationService {

    @Autowired
    private RelationMapper relationMapper;
    @Autowired
    private PageUtil pageUtil;
    @Autowired
    private CommonUtils commonUtils;

    @Override
    @Transactional
    public void saveRelation(Relation relation) throws Exception {
        Boolean aBoolean = commonUtils.checkIdCard(relation.getIdCard(),relation.getName());
        if (!aBoolean){
            throw new BizException(ApiResultCode.ID_CARD_AND_NAME_INCONSISTENT.getCode(),ApiResultCode.ID_CARD_AND_NAME_INCONSISTENT.getMessage()+relation.getName());
        }
        //获取性别
        String substring = relation.getIdCard().substring(relation.getIdCard().length() - 2,relation.getIdCard().length()-1);
        int sex =Integer.parseInt(substring) % 2;
        if (sex > 0){
            relation.setSex(1);
        }else {
            relation.setSex(2);
        }

        relation.setRelation(0);
        relation.setUid(0);
        relation.setStatus(0);
        relation.setUseNumber(0);
        if (relation.getSource()==2){
            Relation relationByIdCardAndSource = relationMapper.getRelationByIdCardAndSource(relation);
            if (relationByIdCardAndSource != null){
                throw new BizException(ApiResultCode.ERROR_RELATION.getCode(),ApiResultCode.ERROR_RELATION.getMessage()+"::"+relation.getName());
            }
            relation.setFreeNumber(-1);
        }
        if (relation.getSource()==3){
            Relation relationByIdCardAndSource = relationMapper.getRelationByIdCardAndSource(relation);
            if (relationByIdCardAndSource != null){
                throw new BizException(ApiResultCode.ERROR_RELATION.getCode(),ApiResultCode.ERROR_RELATION.getMessage()+"::"+relation.getName());
            }
            //获取岁数
            int age = commonUtils.getAgeByIdCard(relation.getIdCard());
            if (age < 70 ){
                relation.setFreeNumber(1);
            }else if ( age >= 80){
                relation.setFreeNumber(3);
            }else {
                relation.setFreeNumber(2);
            }
        }
        if (relation.getSource()==5){
            relation.setFreeNumber(0);
        }
        relationMapper.insertRelation(relation);
    }

    @Override
    public void removeRelation(Ids ids) {
        if (BizEmpty(ids.getId())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        if (BizEmpty(ids.getValue())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        relationMapper.deleteRelation(ids);
    }

    @Override
    public void updateRelation(Relation relation) {
        if (BizEmpty(relation.getId())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        relationMapper.updateRelation(relation);
    }

    @Override
    public Object getRelation(Relation relation) {
        if (BizEmpty(relation.getId())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        RelationDto relation1 = relationMapper.getRelation(relation);
        int age = commonUtils.getAgeByIdCard(relation1.getIdCard());
        relation1.setAge(age);
        return relation1;
    }

    @Override
    public PageInfo<RelationDto> listRelationPage(Relation relation) {
        PageHelper.startPage(pageUtil.getPage().getPageNum(),pageUtil.getPage().getPageSize());
        List<RelationDto> relations = relationMapper.listRelation(relation);
        for (RelationDto relationDto : relations) {
            int age = commonUtils.getAgeByIdCard(relationDto.getIdCard());
            relationDto.setAge(age);
        }
        return new PageInfo<>(relations);
    }
}
